﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

using DragDraw;
using DragDraw.UserService;

namespace DragDraw.Util
{
    /// <summary>
    /// 管理空间资源
    /// 
    /// </summary>
    public class SpaceManager
    {
        public const int UPDATE_INTERVAL = 30;
        public const int STATUS_INIT = 1;
        public const int STATUS_MYSPACE = 2;
        public const int STATUS_NOT_MYSPACE = 3;

        private IWidgetManager m_widgetManager;
        private ITaskBar m_taskBar;

        private List<UserControl> m_controls = new List<UserControl>();

        private DispatcherTimer m_widgetTimer;

        public SpaceManager()
        {
            m_widgetTimer = new DispatcherTimer();
            m_widgetTimer.Interval = new TimeSpan(0, 0, UPDATE_INTERVAL);// 10 secs
            m_widgetTimer.Tick += AutoUpdateWidgets;
        }

        public void SetWidgetManager(IWidgetManager widgetManager)
        {
            ErrorManager.Instance.LogMessage("SpaceManager::SetWidgetManager", "Invoke");
            m_widgetManager = widgetManager;
        }

        public void SetTaskBar(ITaskBar taskbar)
        {
            ErrorManager.Instance.LogMessage("SpaceManager::SetTaskBar", "Invoke");
            m_taskBar = taskbar;
        }

        /// <summary>
        /// 重置SpaceManager
        /// </summary>
        public void SetStatus(int status)
        {
            ErrorManager.Instance.LogMessage("SpaceManager::SetStatus", "Invoke");
            switch (status)
            {
                case STATUS_INIT:
                    m_widgetTimer.Stop();
                    if(OperationManager.Instance.IsInMySpace)
                        AutoUpdateWidgets(null,null);
                    ClearWidgets();
                    m_taskBar.SetStatus(status);
                    break;
                case STATUS_MYSPACE:
                    m_taskBar.SetStatus(status);
                    m_widgetManager.SetBackground(null);
                    m_widgetTimer.Start();
                    break;
                case STATUS_NOT_MYSPACE:
                    m_taskBar.SetStatus(status);
                    m_widgetManager.SetBackground(null);
                    m_widgetTimer.Start();
                    break;
                default:
                    ErrorManager.Instance.ShowDebugMessage("SpaceManager::SetStatus Fail");
                    break;
            }
        }

        /// <summary>
        /// 清除空间管理器所有Widget
        /// </summary>
        public void ClearWidgets()
        {
            ErrorManager.Instance.LogMessage("SpaceManager::ClearWidgets", "Invoke");
            m_widgetManager.HideModalDialog();
            foreach (UserControl uc in m_controls)
            {
                m_widgetManager.DelContainer(uc);
            }
            m_controls.Clear();
        }

        /// <summary>
        /// 设置一个指定的Widget，实现add,set,del操作
        /// </summary>
        /// <param name="widgetInfo">Widget的信息，如果是新widget，则创建</param>
        /// <param name="userControl">Widget的实体控件，如果为null，则删除</param>
        public void SetWidget(WidgetInfo widgetInfo, UserControl userControl)
        {
            ErrorManager.Instance.LogMessage("SpaceManager::SetWidget", "Invoke");
            if (m_controls.Contains(userControl) == false && userControl != null)
            {
                m_controls.Add(userControl);
                m_widgetManager.AddContainer(widgetInfo.WidgetID, userControl);
            }
            if (userControl != null)
            {
                m_widgetManager.SetContainer(userControl,
                    widgetInfo.PosX, widgetInfo.PosY,
                    widgetInfo.Width == 0 ? userControl.Width : widgetInfo.Width,
                    widgetInfo.Height == 0 ? userControl.Height : widgetInfo.Height,
                    OperationManager.Instance.WidgetLibs[widgetInfo.WidgetID].WidgetName);
            }
            else
            {
                m_widgetManager.DelContainer(
                    OperationManager.Instance.Controls[widgetInfo.WidgetID]);
            }
        }

        /// <summary>
        /// 显示一个对话框
        /// </summary>
        /// <param name="widget">对话框包含的widget，null时则使对话框消失</param>
        public void ShowModalDialog(UserControl widget)
        {
            ShowModalDialog(widget, "");
        }

        /// <summary>
        /// 显示一个对话框
        /// </summary>
        /// <param name="widget">对话框包含的widget，null时则使对话框消失</param>
        /// <param name="title">标题</param>
        public void ShowModalDialog(UserControl widget, string title)
        {
            ErrorManager.Instance.LogMessage("SpaceManager::ShowModalDialog", "Invoke");
            if (widget == null)
                m_widgetManager.HideModalDialog();
            else
                m_widgetManager.ShowModalDialog(widget, title);
        }

        private void AutoUpdateWidgets(object sender, EventArgs e)
        {
            //ErrorManager.Instance.ShowDebugMessage("Auto update widgets");
            List<IWidgetContainer> containers = m_widgetManager.Containers;
            foreach (IWidgetContainer c in containers)
            {
                if (c.IsDirty)
                {
                    OperationManager.Instance.SetWidgetInfo(c.WidgetID, (int)c.PosX,
                        (int)c.PosY, (int)c.SizeX, (int)c.SizeY);
                    c.IsDirty = false;
                }
            }
        }
    }

    /// <summary>
    /// 小工具管理器接口，由Silverlight控件实现
    /// 使控件可以作为主界面，管理其它小工具容器
    /// </summary>
    public interface IWidgetManager
    {
        List<IWidgetContainer> Containers { get; }
        void AddContainer(int widgetID, UserControl widget);
        void SetContainer(UserControl widget,
            double posX, double posY, double sizeX, double sizeY, string title);
        void DelContainer(UserControl widget);
        void ShowModalDialog(UserControl widget, string title);
        void HideModalDialog();
        void SetBackground(UserControl widget);
    }

    /// <summary>
    /// 小工具容器接口，由Silverlight控件实现
    /// 使控件能够嵌入小工具并具有移动、缩放、隐藏能力
    /// </summary>
    public interface IWidgetContainer
    {
        bool IsDirty { get; set; }
        double PosX { get; set; }
        double PosY { get; set; }
        double SizeX { get; set; }
        double SizeY { get; set; }
        bool Show { get; set; }
        int WidgetID { get; set; }
        UserControl Widget { get; set; }
        string Title { get; set; }
    }

    /// <summary>
    /// 任务栏接口，由Silverlight控件实现
    /// 使控件能作为任务栏，能够添加快捷按钮
    /// </summary>
    public interface ITaskBar
    {
        void SetStatus(int status);
        void ShowMessage(string message);
    }
}
